/**
 * Created by lxl on 2017/5/16.
 */
layui.use([ 'form'], function () {
    var form = layui.form();
    $(document).ready(function () {
        var chart = echarts.init($('#report')[0]) ;
        var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {},
            legend: {
                data:['销量']
            },
            xAxis: {
                data: []
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'bar',
                data: [5]
            }]
        };
        chart.setOption(option) ;
        autoIFrameHeight();
        $.get("/customer/list?pageNum=1&pageSize=1000", function (data) {
            if (data.success) {
                createOptions("customerId",data.param.list,"id","name") ;
                form.render() ;
            } else {
                alert(data.error);
            }
        });
        $.get("/report/list?type=1", function (data) {
            if (data.success) {
                var xData = [] ;
                var sData = [] ;
                $.each(data.param.list,function(i,val){
                    xData.push(val.name) ;
                    sData.push(val.data) ;
                }) ;
                chart.setOption({
                    xAxis:{
                        data:xData
                    },
                    series: [{
                        name: '销量',
                        data:sData
                    }]
                }) ;
            } else {
                alert(data.error);
            }
        });

        form.on('select(select)',function(data){
            var customerId = $('#customerId').val() ;
            var type = $('#type').val() ;
            var url = "/report/list?type="+type ;
            if(customerId!=0){
                url+="&customerId="+customerId ;
            }
            $.get(url, function (data) {
                if (data.success) {
                    var xData = [] ;
                    var sData = [] ;
                    $.each(data.param.list,function(i,val){
                        xData.push(val.name) ;
                        sData.push(val.data) ;
                    }) ;
                    chart.setOption({
                        xAxis:{
                            data:xData
                        },
                        series: [{
                            name: '销量',
                            data:sData
                        }]
                    }) ;
                } else {
                    alert(data.error);
                }
            });
        })
    });
});